package com.mycompany.app.common.scheduler.model;

import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

import com.mycompany.app.common.entity.ToStringEntity;

import edu.emory.mathcs.backport.java.util.Collections;

@SuppressWarnings( { "serial", "unchecked" })
@Entity
@Table(name = "daemon_job")
public class DaemonJob extends ToStringEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private String name;

    @Column(name = "job_type")
    private String jobType;

    private String description;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "daemonJob", fetch = FetchType.EAGER)
    private Set<DaemonJobTrigger> triggers = Collections.emptySet();

    // ========================================================

    @Override
    public String toString() {

        return ToStringBuilder.reflectionToString(this,
                ToStringStyle.SHORT_PREFIX_STYLE);
    }

    // ========================================================
    public Long getId() {

        return id;
    }

    public void setId(Long id) {

        this.id = id;
    }

    public String getName() {

        return name;
    }

    public void setName(String name) {

        this.name = name;
    }

    public String getDescription() {

        return description;
    }

    public void setDescription(String description) {

        this.description = description;
    }

    public Set<DaemonJobTrigger> getTriggers() {

        return triggers;
    }

    public void setTriggers(Set<DaemonJobTrigger> triggers) {

        this.triggers = triggers;
    }

    // ======================================================================================
    public Set<DaemonJobTrigger> addSchedule(DaemonJobTrigger schedule) {

        this.triggers.add(schedule);
        return triggers;
    }

    public DaemonJob(Long id) {

        super();
        this.id = id;
    }

    public DaemonJob() {

    }

    public String getJobType() {

        return jobType;
    }

    public void setJobType(String jobType) {

        this.jobType = jobType;
    }

}
